﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hmc.IM.Common.Forms;
using System.Runtime.InteropServices;
using Hmc.IM.Common.UserControls;

namespace Hmc.IM.Common.Forms
{
    /// <summary>
    /// POS弹出窗体
    /// </summary>
    public partial class PosDialogForm : Form
    {
        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;

        /// <summary>
        /// 系统API函数，发送消息
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="Msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImportAttribute("user32.dll")]
        static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        /// <summary>
        /// 系统API函数，移动窗体
        /// </summary>
        /// <returns></returns>
        [DllImportAttribute("user32.dll")]
        static extern bool ReleaseCapture();

        [DllImport("gdi32.dll")]
        static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2,
        int cx, int cy);

        [DllImport("user32.dll")]
        static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {

            base.OnResize(e);

            IntPtr Rgn = CreateRoundRectRgn(1, 1, this.Width - 1, this.Height - 1, 7, 7);

            SetWindowRgn(this.Handle, Rgn, true);

        }

        /// <summary>
        /// 标题
        /// </summary>
        public string TitleText
        {
            get
            {
                return lbTitle.Text;
            }
            set
            {
                lbTitle.Text = value;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public PosDialogForm()
        {
            InitializeComponent();
            this.ImeMode = System.Windows.Forms.ImeMode.KatakanaHalf;
            //this.TransparencyKey = this.BackColor;
            plTop.MouseDown += new MouseEventHandler(pTitle_MouseDown);
            lbTitle.MouseDown += new MouseEventHandler(pTitle_MouseDown);
            this.Width = base.Width;
            this.Height = base.Height;
        }

        /// <summary>
        /// 鼠标点击移动窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        /// <summary>
        /// 验证指定集合控件下的数据格式
        /// </summary>
        /// <param name="controls">需验证的数据集合</param>
        /// <returns></returns>
        public bool CheckValidate(Control controls)
        {
            bool fay = true;
            foreach (Control ctl in controls.Controls)
            {
                if (ctl is IValidate)
                {
                    IValidate box = ctl as IValidate;
                    if (!box.Validate())
                    {
                        fay = false;
                        break;
                    }
                }
                else if (ctl.Controls.Count > 0)
                {
                    CheckValidate(ctl);
                }
            }
            return fay;
        }

        /// <summary>
        /// 验证当前窗体集合下的数据格式
        /// </summary>
        /// <returns></returns>
        public bool CheckValidate()
        {
            return CheckValidate(this);
        }
    }
}
